﻿Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub
    Private Function GetDirectorySize(ByVal Path As String, ByVal Size As Double) As Double

        Dim dir As New DirectoryInfo(Path)
        Dim fileSize As Double = 0
        Dim dirSize As Double = 0

        For Each fi As FileInfo In dir.GetFiles()
            'MessageBox.Show(fi.Name + ": " + fi.Length.ToString)
            Size = Size + fi.Length
        Next

        For Each di As DirectoryInfo In dir.GetDirectories()
            'MessageBox.Show("Entering SubDir: " + di.FullName)
            'MessageBox.Show("Size up to now: " + Size.ToString)
            Size = Size + GetDirectorySize(di.FullName, 0)
        Next

        Return Size

    End Function

    
    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim dir As New DirectoryInfo(TextBox1.Text)

        Dim dt1 As New DataTable
        dt1.Columns.Add("Directory Name", GetType(System.String))
        dt1.Columns.Add("Size", GetType(System.Double))
        Dim dt2 As New DataTable
        dt2.Columns.Add("Directory Name", GetType(System.String))
        dt2.Columns.Add("Size", GetType(System.Double))
        Dim dt3 As New DataTable
        dt3.Columns.Add("Directory Name", GetType(System.String))
        dt3.Columns.Add("Size", GetType(System.Double))

        For Each di As DirectoryInfo In dir.GetDirectories()
            Dim SizeInGB As Double = (((GetDirectorySize(di.FullName, 0) / 1024) / 1024) / 1024)

            Dim dr As DataRow
            dr = dt1.NewRow
            dr(0) = di.Name
            dr(1) = Math.Round(SizeInGB, 2)
            dt1.Rows.Add(dr)
        
            If SizeInGB > 3.5 Then
                dr = dt2.NewRow
                dr(0) = di.Name
                dr(1) = Math.Round(SizeInGB, 2)
                dt2.Rows.Add(dr)
            End If
            If SizeInGB > 4.5 Then
                dr = dt3.NewRow
                dr(0) = di.Name
                dr(1) = Math.Round(SizeInGB, 2)
                dt3.Rows.Add(dr)
            End If
        Next
        DataGridView1.DataSource = dt1
        DataGridView2.DataSource = dt2
        DataGridView3.DataSource = dt3
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = My.Settings.Directory
        FolderBrowserDialog1.SelectedPath = My.Settings.Directory
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        My.Settings.Directory = TextBox1.Text
    End Sub
End Class
